<style lang="scss">
@import '@/assets/css/variable';
.searchResult{
	background-image: linear-gradient(to bottom, #003792 , #01617a);
}
</style>

<template>
  <div class="searchResult wrapperContent2" ref="searchResult" @scroll="scrollBottom">
	<!-- <navbar></navbar> -->
	<div class="padding-all-l" @click="$router.replace('/index')">
		<van-icon name="arrow-left" color="#ffffff" size="20"/>
	</div>
	<div v-if="count>0" :style="{fontSize:fontSize==1?'18px':'21px'}">
		<div class="fc-fff padding-lr-l line-height-l letter-s-1">
			已为您{{$route.query.type=='search'?'搜索':'匹配'}}相关政策
		</div>
		<div class="fc-fff padding-lr-l line-height-l letter-s-1">
			共{{count}}个
		</div>
	</div>
	<my-list :list="listCatchData[active]" :load="load[active]"></my-list>
  </div>
</template>
<script>
import myList from '@/components/list/IndexList.vue'
import navbar from '@/components/Navbar.vue'
import { myMixins } from "@/assets/js/mixins.js";
export default {
	name:'searchResult',
	mixins: [myMixins],
  components: {myList,navbar},
  data() {
    return {
		searchText:'',
		active:0,
		policy_keywords:'',
		is_validity: 1,
		// pageSize:10,
		// currPage:1
		listCatchData:{},
		load:{},
		pageSize:100,
		count:0
    };
  },
  created() {
    // document.title = "网络学习首页";
  	this.bus.$on('scrollToTop4Result',()=>{
  		console.log("scrollToTop4Result")
  		// this.$refs.policy.scrollTo(0,localStorage.getItem('scrollTop'))
  		this.$refs.searchResult.scrollTop = localStorage.getItem('scrollTop4Result')
  	})
	this.bus.$on('refreshSearchResult',()=>{
		console.log('=================refreshSearchResult=========================',localStorage.getItem('searchText'))
		this.policy_keywords = localStorage.getItem('searchText')||this.$route.query.searchText
		this.listCatchData[0] = []
		this.count = 0
		this.getList(0)
	})
  },
  beforeDestroy() {
  	console.log("off scrollToTop4Result scrollToTop4Result beforeDestroy")
  	this.bus.$off('scrollToTop4Result')
	this.bus.$off('refreshSearchResult')
  },
  mounted() {
	  console.log('=================mounted====================')
	this.policy_keywords = this.$route.query.searchText
	console.log("policy_keywords",this.policy_keywords)
	this.listCatchData[0] = []
	this.getList(0)
  },
  methods: {
	  unitsPolicyList(current) {
	  	if(!this.load[current]){
	  		this.load[current] = {
	  			page:1,
	  			loading:'loading'
	  		}
	  	}
	    const params = {
	  	  policy_keywords: this.policy_keywords,
	  	  is_validity: this.is_validity,
	  	  pageSize: this.pageSize,
	  	  currPage: this.load[current].page
	    }
	    console.log('params',params)
	    this.$request.unitsPolicyList(params).then(res=>{
	  	console.log('res',res)
	    });
	  },
	getList(current) {
		if(!this.load[current]){
			this.load[current] = {
				page:1,
				loading:'loading'
			}
		}
		console.log('getList__policy_keywords',this.$route.query.searchText)
		// localStorage.getItem('searchText')||
		if(!this.$route.query.searchText) return
		const params = {
			policy_keywords: this.$route.query.searchText,
			is_validity: this.is_validity,
			search:1,
			currPage:this.load[current].page,
			pageSize:this.pageSize
		}
		console.log('getList__params',params)
		this.$request.unitsPolicyList(params).then(res => {
			const data = res.data.list
			this.count = res.data.page.count
			//如果没数据了
			if(data.length === 0 || data.length < this.pageSize){
				let oldLoad = {}
				oldLoad.loading = 'noMore'
				oldLoad.page = this.load[current].page
				this.load[current] = oldLoad
			}
			if(data.length == this.pageSize) {
				let oldLoad = {}
				oldLoad.loading = 'loadEnd'
				oldLoad.page = this.load[current].page
				this.load[current] = oldLoad
			}
			if(data.length>0){
				let oldList = this.listCatchData[current] || []
				oldList.push(...data)
				this.listCatchData[current] = oldList
			}
		}).finally(res=>{
			if(!this.listCatchData[current]||this.listCatchData[current].length<=0){
				this.load[current].loading='noData'
			}
			// this.bus.$emit('refreshList',this.load[current])
			console.log('this.load[current]',this.load[current])
			this.$forceUpdate()
		})
	},
    searchBtn() {
		console.log("search")
	},
	scrollBottom(e) {
		// console.log('e',e)
		const {scrollTop, clientHeight, scrollHeight} = e.target
		// console.log(scrollTop, clientHeight, scrollHeight)
		localStorage.setItem('scrollTop4Result',scrollTop)
		if (scrollTop + clientHeight === scrollHeight){
			console.log('滚动到底部啦')
		}
	},
  }
}

</script>